var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
var isHasMg = "0";
$CertusFormAjax.ajax("findLxInfo",{"processId":processId}, function(result){
    var data = result.data.map;
    $$scope.proNo = data.pro_no;
    $$scope.proArea = data.pro_area;
    var proType = data.pro_type;
    $$scope.typeCode=proType;
    if(proType=="ZL"){
        $$scope.proType="战略类";
    }else if (proType=="YF"){
        $$scope.proType="研发类";
    }else if (proType=="XS"){
        $$scope.proType="销售类";
        $$scope.isXS=true;
    }else if (proType=="BM"){
        $$scope.proType="部门类";
    }else if (proType=="SW"){
        $$scope.proType="事务类";
    }else if (proType=="CS"){
        $$scope.proType="测试类";
    }
    $$scope.proName = data.pro_name;
    $("#lxLevel").val(data.pro_lv);
    //如果是 自己勾选的项目经理，则不能更改
    if (data.is_mg=="true"){
        $("#autocompleteMg").attr("disabled",true);
    };

    if (proType=="XS"){
        $$scope.showSign = true;
        $$scope.showHk = true;
        $$scope.showArea = true;
        $$scope.showCus = true;
        $$scope.isCompare=true;
        $$scope.showCompe = true;
        $$scope.showPolucy = true;
        $$scope.proArea = data.pro_area;
        var cusName = data.cus_name.split("-----");
        $$scope.cusName = cusName[0];
        if(data.is_compare=="0"){
            $$scope.isCompare = "无";
        }else{
            $$scope.isCompare = "有";
        }
        $$scope.divineSignDate = data.divine_sign_date;
        $$scope.divineHkDate = data.divine_hk_date;
        $$scope.proSelId =data.selId;
        $$scope.proSelName = data.selName+"("+data.selId+")"+"["+data.bsPositonName+"]";
    }else if (proType=="ZL"||proType=="SW"
        ||proType=="BM"){
        $$scope.showFinish = true;
        $$scope.isCompare=false;
        $$scope.divineFinishTime = data.divine_finish_time;
    }else {
        $$scope.showVersion = true;
        $$scope.isCompare=false;
        $$scope.divineVersionTime = data.divine_version_time
    }

    if (data.urgency==1){
        $$scope.urgency="紧急";
    }else{
        $$scope.urgency="不紧急";
    }

    if (data.importance==0){
        $$scope.importance="一般";
    }else if (data.importance==1){
        $$scope.importance="重要";
    }else{
        $$scope.importance="战略";
    }

    $$scope.clueDesc = data.clue_desc;
    $$scope.cusPolicy = data.cus_policy;
    $$scope.competitor = data.competitor;
    $$scope.lxDesc = data.lx_desc;
    $$scope.proTarget = data.pro_target;
    $$scope.result = data.result;
    $$scope.divineMoney = data.divine_money;
    $$scope.cusBudget = data.cus_budget;
    $$scope.startTime = data.start_time;
    $$scope.endTime = data.end_time;
    $$scope.deptNames = result.data.deptNames;
    $$scope.proId = data.pro_id;
    $$scope.lxId = data.id;
    $$scope.opinionMsg = data.opinion_msg;
    $$scope.proLv = data.pro_lv+"";

    $("#lxLevel").val(data.pro_lv);
    if (data.pro_lv=="3"){
        $$scope.lxLevel = "高";
    }else if (data.pro_lv=="2"){
        $$scope.lxLevel = "中";
    }else {
        $$scope.lxLevel = "低";
    }

    $$scope.selName = data.selName;
    $$scope.delName = data.delName+"("+data.bdId+")"+"["+data.delPositonName+"]";
    $$scope.pduName = data.pduName+"("+data.pduId+")"+"["+data.pduPositonName+"]";
    var mgId = data.pro_mg_id;
    if (mgId==null||mgId==""||mgId==undefined){
        $("#isHide").hide();
        //未指定经理
        $("#isMgId").empty();
        $("#isMgId").html('' +
            '<input type="text" id="autocompleteMg" class="form-control">\n' +
            '<input type="hidden" value="" name="proMgId" id="proMgId"/>');
        isHasMgId(false);
    }else{
        $$scope.isResult = true;
        //指定项目经理
        $$scope.proMgId=mgId;
        $$scope.userName = data.user_name+"("+data.pro_mg_id+")"+"["+data.positon_name+"]";
        $$scope.divineContactMoney = data.divine_contact_money;
        $$scope.divineBusinessMoney = data.divine_business_money;
        $$scope.divinePurchaseMoney = data.divine_purchase_money;
        isHasMgId(true);
        isHasMg="1";
        $$scope.mgTs = "（由"+data.mgName+"担任项目经理）";
        $$scope.isMgId = true;
        var arr = result.data.bugetList;
        var other1Columns = [{
            id: 'bugetPhase',
            title: '阶段',
            width: 100,
            type: 'string',
            columnClass: 'text-center',
        },{
            id: 'bugetType',
            title: '预算类型',
            type: 'string',
            width: 100,
            columnClass: 'text-center',
        }, {
            id: 'buget',
            title: '预算金额',
            type: 'string',
            width: 150,
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
                var content = '￥' + value + '元';
                return content;
              }
        },{
            id: 'budgetBegin',
            title: '开始时间',
            type: 'string',
            width: 100,
            columnClass: 'text-center',
            resolution: function (value) {
                var content = '';
                if (value==""){
                    return content;
                }else{
                    var date = new Date(value);
                    content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                    return content;
                }
            }
        },{
            id: 'budgetEnd',
            title: '结束时间',
            type: 'string',
            width: 100,
            columnClass: 'text-center',
            resolution: function (value) {
                var content = '';
                if (value==""){
                    return content;
                }else{
                    var date = new Date(value);
                    content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                    return content;
                }

            }
        },{
            id: 'comments',
            title: '费用说明',
            type: 'string',
            columnClass: 'text-left'
        }];
        var other1Option = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '其他费用',
            datas: arr,
            columns: other1Columns,
            gridContainer: 'other1Container',
            tools: ''
        };
        var other1Grid = $.fn.dlshouwen.grid.init(other1Option);
        other1Grid.load();


        var proinColumns = [{
            id: 'wntyName',
            title: '指标名称',
            width: 150,
            type: 'string',
            columnClass: 'text-center',
        }, {
            id: 'wntyDesc',
            title: '指标描述',
            type: 'string',
            columnClass: 'text-left',
        }, {
            id: 'finishTime',
            title: '完成时间',
            type: 'string',
            width: 100,
            columnClass: 'text-left',
        }];
        var warrList = result.data.warrList;
        var proinOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '项目指标',
            datas:warrList ,
            columns: proinColumns,
            gridContainer: 'proinContainer',
            tools: ''
        };
        var proinGrid = $.fn.dlshouwen.grid.init(proinOption);
        proinGrid.load();

        var prointColumns = [{
            id: 'proTarget',
            title: '项目目标',
            type: 'string',
            columnClass: 'text-left',
        },{
            id: 'finishTime',
            title: '完成时间',
            type: 'date',
            width: 100,
            format:'yyyy-MM-dd',
            otype:'time_stamp_ms',
            columnClass: 'text-center',
        }];
        var targetList = result.data.targetList;
        var prointOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '项目目标',
            datas:targetList ,
            columns: prointColumns,
            gridContainer: 'prointContainer',
            tools: ''
        };
        var prointGrid = $.fn.dlshouwen.grid.init(prointOption);
        prointGrid.load();

        //人天预估
        var rlColumns = [{
            id: 'roleName',
            title: '角色',
            type: 'string',
            columnClass: 'text-center'
        },{
            id: 'level',
            title: '级别',
            type: 'string',
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
                var content = '';
                if (value=="1"){
                    content = "低";
                }else if (value=="2"){
                    content = "中";
                }else{
                    content = "高";
                }
                return content;
            }
        },{
            id: 'beginTime',
            title: '开始时间',
            type: 'date',
            format: 'yyyy-MM-dd',
            otype: 'time_stamp_ms',
            columnClass: 'text-center',
        }, {
            id: 'endTime',
            title: '结束时间',
            type: 'date',
            format: 'yyyy-MM-dd',
            otype: 'time_stamp_ms',
            columnClass: 'text-center',
        },{
            id: 'rtNum',
            title: '人天',
            type: 'string',
            columnClass: 'text-center',
        }, {
            id: 'comments',
            title: '说明',
            type: 'string',
            columnClass: 'text-center',
        }];
        var rlOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '人力成本列表',
            datas: result.data.rtList,
            columns: rlColumns,
            gridContainer: 'rtContainer',
        };
        var rlGrid = $.fn.dlshouwen.grid.init(rlOption);
        rlGrid.load();

        //涉及文件
        var wjColumns = [{
            id: 'productName',
            title: '文件名称',
            type: 'string',
            columnClass: 'text-center'
        }, {
            id: 'comments',
            title: '说明',
            type: 'string',
            columnClass: 'text-center',
        }];
        var wjOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '人力成本列表',
            datas: result.data.wjList,
            columns: wjColumns,
            gridContainer: 'wjContainer'
        };
        var wjGrid = $.fn.dlshouwen.grid.init(wjOption);
        wjGrid.load();
    }
});

function isHasMgId(flag) {
    $CertusFormAjax.ajax("findSpUserList",{},function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].userName+"["+data[i].positonName+"]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // if(!flag){
        //     //项目经理
        //
        // }

        $('#autocompleteMg').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proMgId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });

        //产品经理
        $('#autocompletePdu').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proPduId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });

        //交付经理
        $('#autocompleteDel').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proDelId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });


    });
};
//项目其他费用表单显示隐藏
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};
//申请通过
$(".submit").on('click',function () {
    if ($("#proMgId").val()==""){
        layer.msg("请选择项目经理!");
        return;
    }
    if ($("#proPduId").val()==""){
        layer.msg("请选择产品经理!");
        return;
    }
    if ($("#proDelId").val()==""){
        layer.msg("请选择交付经理!");
        return;
    }
    if ($("#lxLevel").val()==0){
        layer.msg("请选择项目优先级!");
        return;
    }
    var params ={};
    if($$scope.typeCode=="XS"){
        params.proDelId = $("#proDelId").val();
        params.proPduId = $("#proPduId").val();
        params.proSelId = $("#proSelId").val();
    }
    if ($$scope.typeCode=="BM"){
        params.lxLevel=2;
    }
    params.optionMsg = $("#optionMsg").val();
    params.processId = processId;
    params.stepId = stepId;
    params.lxLevel = $("#lxLevel").val();
    params.proMgId = $("#proMgId").val();
    params.proId = $("#proId").val();
    params.lxId = $("#lxId").val();
    params.isHasMg = isHasMg;
    params.proType= $$scope.typeCode;
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('toSpLxInfo',{'params':JSON.stringify(params)},function () {
        //关闭
       layer.close(index);
       layer.msg("审批成功");
       window.history.back()
    });
});
//申请不通过
$(".unSubmit").on('click',function () {
    var params ={};
    params.optionMsg = $("#optionMsg").val();
    params.processId = processId;
    params.stepId = stepId;
    params.lxLevel = $("#lxLevel").val();
    params.proId = $("#proId").val();
    params.lxId = $("#lxId").val();
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('toSpLxInfo2',{'params':JSON.stringify(params)},function () {
        //关闭
        layer.close(index);
        layer.msg("审批成功");
        window.history.back()
    });
});
//申请退回
$(".rSubmit").on('click',function () {
    var params ={};
    params.optionMsg = $("#optionMsg").val();
    params.processId = processId;
    params.stepId = stepId;
    params.lxLevel = $("#lxLevel").val();
    params.proId = $("#proId").val();
    params.lxId = $("#lxId").val();
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('rhLxSq',{'params':JSON.stringify(params)},function () {
        //关闭
        layer.close(index);
        layer.msg("审批成功");
        window.history.back()
    });
})